import json
import datetime
import os
from collections import Counter
import csv
import time
import datetime
# ht=[ '#YearOnTikTok', '#lovestory','#artmas','#festivefashion','#happyhanukkah','#planttiktok','#productivity','#ImAGhost','#catchphrases','#ootd','#comfortfood','#holidaycrafts','#watchmegrow','#holidaydecor','#raisedby','#makeitvogue','#goodmorning','#watchingxfactor','#catchphrases','#rnbvibes','#gamenight','#HorrorToK', '#ProblemSolved', '#SkateLife', '#ThriftShop', '#SpookyTreats',  '#tiktokfood', '#interiordesign', '#sfxmakeup', '#ComingOfAge', '#falldiy', '#worldseries', '#gaminglife', '#meleaving', '#yellow', '#yougotthis','#mycostume','#mypfp','#welldone','#nativefamily','#happyhalloween','#homeoffice','#myrecommendation','#myhobby','#fanedit','#stemlife','#motivationmonday','#wip','#veteransday','#holidaycountdown','#WeekendVibes','#holidaysourway','#RoomTour','#bakingszn','#nbadraft',
#      '#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#fallguysmoments','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok','#holidaymusic','#howbizarre', '#personalfinance'
#      ,'#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion','graphicdesign','smallbusiness','#halloweenlook','fallfashion','#artmas','#haventseen','#planttiktok','#productivity','#bekind','#onhold','#growupwithme','#tabletop','#theatrekids','#wishlist','#haventseen','#selfimprovement', '#OhNo','#animation']
ht=['#HolidaysOurWay']
ht=[h.lower().replace('#','') for h in ht]


for hashtag in ht:
    c=Counter()
    datalist=[]
    dict = {}
    for name in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if hashtag in name.lower():
            datalist.append(name)
    with open('D:\\Work\\Tool\\tiktok\\authorstats'+hashtag+'.tsv', 'w', encoding='utf-8',
              newline='\n') as filename_output:
        writer = csv.writer(filename_output, delimiter='\t')
        row=['id','followingCount','followerCount','heartCount','videoCount','diggCount']
        print(len(row))
        writer.writerow(row)
        print(row)
        for i in range(len(datalist)):
            ranking=0
            with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + datalist[i], 'r', encoding='utf-8', newline='\n') as filename_input:
                f=[]
                for line in filename_input:
                    z = json.loads(line)
                    if 'id' in z.keys():
                        id=z['id']

                    elif 'itemInfos' in z.keys():
                        id=z['itemInfos']['id']
                    if id in dict.keys():
                        continue
                    else:
                        following = z['authorStats']['followingCount']
                        follower = z['authorStats']['followerCount']
                        heart = z['authorStats']['heartCount']
                        video = z['authorStats']['videoCount']
                        digg = z['authorStats']['diggCount']
                        dict[id]=[following,follower,heart,video,digg]
        j=0
        for key in dict.keys():
            rows=[key]
            rows.extend(dict[key])
            if j<100:
                print(len(rows))
                print(rows)
                j+=1
            writer.writerow(rows)


    # dict[hashtag] = [datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'),
    #                  datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'), datalist[0].split('_')[2]]
    #
    # print(hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2])
    # writer.writerow([hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2]])